﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.EmpbasicService;
using CommunitySystem.DiaoDongService;
using System.Collections.ObjectModel;
using EHR.Common;

namespace CommunitySystem
{
    
    public partial class EMPSBWindow : ChildWindow
    {
        private EmpBasicServiceClient empclient = new EmpBasicServiceClient();
        private DiaoDongServiceClient dd = new DiaoDongServiceClient();
        private ZJZZR_Emp _empzr = new ZJZZR_Emp();
        public EMPSBWindow(ZJZZR_Emp empzr)
        {
            InitializeComponent();
            _empzr = empzr;
            this.DataContext = _empzr;
           
            List<string> zwsx = new List<string>() { "领导职务", "非领导职务" };
            this.cbzwsx.ItemsSource = zwsx;
            if (_empzr.ZWSX == "领导职务")
            {
                this.cbzwsx.SelectedIndex = 0;
                this.spld.Visibility = Visibility.Visible;
            }
            if (_empzr.ZWSX == "非领导职务")
            {
                this.cbzwsx.SelectedIndex = 1;
                this.spld.Visibility = Visibility.Collapsed;
            }

            this.cbzwsx.SelectionChanged += new SelectionChangedEventHandler(cbzwsx_SelectionChanged);

            //科室
            empclient.GetBianZhiModelCompleted += new EventHandler<EmpbasicService.GetBianZhiModelCompletedEventArgs>(Client_GetBianZhiModelCompleted);
            empclient.GetBianZhiModelAsync(empzr.ZRDWID.ToString(), 0);
       
        }

   
        void cbzwsx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.cbzwsx.SelectedItem != null)
            {
                if (this.cbzwsx.SelectedItem.ToString().Equals("领导职务"))
                    this.spld.Visibility = Visibility.Visible;
                else
                    this.spld.Visibility = Visibility.Collapsed;

                _empzr.ZWSX = this.cbzwsx.SelectedItem.ToString();
            }
        }
        void Client_GetBianZhiModelCompleted(object sender, EmpbasicService.GetBianZhiModelCompletedEventArgs e)
        {
            biaoti.ItemsSource = null;
            biaoti.ItemsSource = e.Result;
            if (biaoti.ItemsSource != null)
            {
                for (int i = 0; i < ((ObservableCollection<BianZhiModel>)biaoti.ItemsSource).Count; i++)
                {
                    if (((ObservableCollection<BianZhiModel>)biaoti.ItemsSource)[i].JiGouMingCheng == _empzr.XKS)
                        biaoti.SelectedIndex = i;
                }
            }
        }
        private int keyongzhishu = 0;
        private BianZhiModel SelKS = null;
        void gangwei_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBox)sender).SelectedItem != null)
            {
                SelKS = new BianZhiModel();
                SelKS=((BianZhiModel)((((ComboBox)sender).SelectedItem)));
                _empzr.XKS = SelKS.JiGouMingCheng;

                //ZSPD();
              
            }

        }

        private int ZSPD()
        {
            if (_empzr.ZWCC == "乡科级正职（主任科员）" && _empzr.ZWSX == "领导职务")
                keyongzhishu = Convert.ToInt32(SelKS.ZhengKeShiJi);
            if (_empzr.ZWCC == "乡科级正职（主任科员）" && _empzr.ZWSX == "非领导职务")
                keyongzhishu = Convert.ToInt32(SelKS.ZhuRenShiJi);
            if (_empzr.ZWCC == "乡科级副职（副主任科员）" && _empzr.ZWSX == "领导职务")
                keyongzhishu = Convert.ToInt32(SelKS.FuKeShiJi);
            if (_empzr.ZWCC == "乡科级副职（副主任科员）" && _empzr.ZWSX == "非领导职务")
                keyongzhishu = Convert.ToInt32(SelKS.FuZhuRenShiJi);

            return keyongzhishu;
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
         
           
            if (string.IsNullOrEmpty(_empzr.TLSJ))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择讨论时间!");
                return;
            }
            if (string.IsNullOrEmpty(_empzr.XKS))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择科室!");
                return;
            }
            if (string.IsNullOrEmpty(_empzr.XZW))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择职务!");
                return;
            }
            if (string.IsNullOrEmpty(_empzr.ZWSX))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择职务属性!");
                return;
            }
            if (!string.IsNullOrEmpty(_empzr.XKS) && !string.IsNullOrEmpty(_empzr.ZWSX))
            {
                if (ZSPD() <= 0)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("职数已满!");
                    return;
                }
            }
            if (string.IsNullOrEmpty(_empzr.FGCSGZ))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("分管从事工作不能为空!");
                return;
            }
            if (!string.IsNullOrEmpty(_empzr.ZWSX))
            {
                if (string.IsNullOrEmpty(_empzr.BegRQ))
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择任期开始时间!");
                    return;
                }
                if (Convert.ToDateTime(_empzr.BegRQ) < Convert.ToDateTime(_empzr.TLSJ))
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("任期开始时间不能早于会议讨论时间！");
                    return;
                }
                if (_empzr.ZWSX == "领导职务")
                {
                    if (string.IsNullOrEmpty(_empzr.EndRQ))
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择任期结束时间!");
                        return;
                    }
                    else if (Convert.ToDateTime(_empzr.EndRQ) < Convert.ToDateTime(_empzr.BegRQ))
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("任期结束时间不能早于开始时间！");
                        return;
                    }
                }
               
            }
           
          
            _empzr.BZ = "已转任";
            _empzr.CreateDate = DateTime.Now.ToString();
            _empzr.CreateUser = Session.LoginUser.LoginName;

            dd.SBZJZZR_EMPCompleted += new EventHandler<SBZJZZR_EMPCompletedEventArgs>(dd_SBZJZZR_EMPCompleted);
            dd.SBZJZZR_EMPAsync(_empzr);
        }

        void dd_SBZJZZR_EMPCompleted(object sender, SBZJZZR_EMPCompletedEventArgs e)
        {
            if (e.Result)
                SilverlightMessageBoxLibrary.Message.InfoMessage("上报成功!");
            else SilverlightMessageBoxLibrary.Message.InfoMessage("上报失败!");

            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

